/**
 * 节点类型
 */
export interface NodeItemType {
	id: string;
	label: string;
	type: string;
	color?: string;
	size?: number;
	backgroundImg?: string;
	gradientStops?: { offset: number; color: string; opacity?: number }[];
	gradientType?: string;
	gradientDir?: number[];
}

export interface LinkItemType {
	source: string;
	target: string;
	label: string;
	isHierarchy: boolean;
}

export interface TreeDataItemType {
	nodes: NodeItemType[];
	links: LinkItemType[];
}

/**
 * 渐变色标类型
 */
export interface GradientStop {
	offset: number;
	color: string;
	opacity?: number;
}

/**
 * 主题颜色配置
 */
export interface ThemeColorType {
	shape?: string;
	color?: string;
	startColor?: string;
	endColor?: string;
	startDir?: number[];
	endDir?: number[];
	gradientStyle?: boolean | 'multi'; // 扩展支持 'multi' 表示多段渐变
	fillColor?: string;
	textColor?: string;
	borderColor?: string;
	borderWidth?: number;
	fontSize?: number;
	fontWeight?: number;
	fontFamily?: string;
	paddingX?: number;
	paddingY?: number;
	marginY?: number;
	marginX?: number;
	borderRadius?: number;
	// 多段渐变相关配置
	gradientStops?: GradientStop[]; // 多段渐变的色标数组
	gradientDir?: [number, number, number, number]; // 渐变方向 [x1, y1, x2, y2]
}

/**
 * 完整主题配置
 */
export interface ThemeConfigType {
	root?: ThemeColorType;
	second?: ThemeColorType;
	node?: ThemeColorType;
	lineWidth?: number;
	lineColor?: string;
	lineStyle?: 'curve' | 'straight' | 'direct';
	rootLineStartPositionKeepSameInCurve?: boolean;
	typeColors?: {
		[type: string]: ThemeColorType;
	};
}

export interface MindMapConfig {
	el: HTMLElement;
	data: { nodes: NodeItemType[]; links: LinkItemType[] };
	themeConfig?: ThemeConfigType;
	mousewheelAction?: 'zoom' | 'move';
	openPerformance?: boolean;
	readonly?: boolean;
}

/**
 * 节点数据的数据类型
 */
export interface TreeNodeItemDataType {
	id?: string;
	uid: string;
	text: string;
	type: string;
	isActive?: boolean;
	expand?: boolean;
}

export interface TreeNodeDataType {
	data: TreeNodeItemDataType;
	children?: TreeNodeDataType[];
}

/**
 * 思维导图节点元素类型（渲染后的节点实例）
 */
export interface TreeNodeElType {
	uid: string;
	parent: TreeNodeElType;
	children: TreeNodeElType[];
	nodeData: TreeNodeDataType;
	isRoot?: boolean;
	group?: {
		node: HTMLElement;
	};
	[key: string]: unknown;
}

/**
 * 节点查找条件类型
 */
export interface NodeFindCondition {
	type: 'id' | 'name' | 'type';
	value: string | string[];
}

/**
 * 多段渐变配置
 */
export interface MultiGradientConfigItem {
	gradientStops: GradientStop[];
	gradientDir: [number, number, number, number];
}

export interface MultiGradientConfig {
	root: MultiGradientConfigItem | null;
	second: MultiGradientConfigItem | null;
	node: MultiGradientConfigItem | null;
}

/**
 * ========== 事件相关类型定义 ==========
 */

/**
 * 鼠标事件类型
 */
export interface CustomMouseEvent {
	clientX: number;
	clientY: number;
	target: EventTarget | HTMLElement | null;
	preventDefault: () => void;
	stopPropagation: () => void;
}

/**
 * MindMap节点事件参数
 */
export interface MindMapNodeEvent {
	uid: string;
	nodeData?: {
		data: Record<string, unknown>;
	};
	parent?: MindMapNodeEvent;
	children?: unknown[];
	[key: string]: unknown;
}

/**
 * MindMap文本编辑事件参数
 */
export interface MindMapTextEditElement {
	style?: {
		display?: string;
		visibility?: string;
		opacity?: string | number;
		[key: string]: unknown;
	};
}

/**
 * 数据变化事件数据
 */
export interface DataChangeEvent {
	[key: string]: unknown;
}

/**
 * 节点上下文菜单事件参数
 */
export interface NodeContextMenuEvent {
	action: string;
	nodeId?: string;
	nodeText?: string;
	warning?: string;
	error?: string;
	timestamp: string;
	currentNodeId?: string;
	currentNodeText?: string;
	currentNodeType?: string;
	parentNodeId?: string;
	parentNodeText?: string;
	parentNodeType?: string;
	grandParentNodeId?: string;
	grandParentNodeText?: string;
	grandParentNodeType?: string;
}

/**
 * ========== API方法参数类型定义 ==========
 */

/**
 * 节点数据（用于添加节点）
 */
export interface NodeDataForAdd {
	id: string;
	label: string;
	type: string;
	[key: string]: unknown;
}

/**
 * 节点数据（用于批量添加节点）
 */
export interface NodeDataForAddPatch {
	nodeData: NodeDataForAdd;
	parentNodeId?: string | null;
}

/**
 * 连接数据（用于添加连接）
 */
export interface LinkDataForAdd {
	source: string;
	target: string;
	[key: string]: unknown;
}

/**
 * 更新节点数据
 */
export interface UpdateNodeData {
	label?: string;
	type?: string;
	[key: string]: unknown;
}
